load("//bazel:fuzz_target.bzl", "java_fuzz_target_test")

java_library(
    name = "test_successful_exception",
    srcs = ["TestSuccessfulException.java"],
    visibility = ["//src/test/java/com/code_intelligence/jazzer/junit:__subpackages__"],
)

java_binary(
    name = "ExampleFuzzTests",
    testonly = True,
    srcs = glob(["*.java"]),
    create_executable = False,
    visibility = [
        "//src/test/java/com/code_intelligence/jazzer/junit:__pkg__",
    ],
    deps = [
        ":test_successful_exception",
        "//deploy:jazzer",
        "//deploy:jazzer-api",
        "//deploy:jazzer-junit",
        "//examples/junit/src/main/java/com/example:parser",
        "//examples/junit/src/test/resources:example_seed_corpora",
        "@maven//:com_google_truth_extensions_truth_java8_extension",
        "@maven//:com_google_truth_truth",
        "@maven//:org_junit_jupiter_junit_jupiter_api",
        "@maven//:org_junit_jupiter_junit_jupiter_params",
        "@maven//:org_mockito_mockito_core",
    ],
)

java_fuzz_target_test(
    name = "DataFuzzTest",
    srcs = ["ValidFuzzTests.java"],
    allowed_findings = ["com.code_intelligence.jazzer.api.FuzzerSecurityIssueMedium"],
    fuzzer_args = [
        "-runs=0",
    ],
    target_class = "com.example.ValidFuzzTests",
    target_method = "dataFuzz",
    verify_crash_reproducer = False,
    runtime_deps = [
        ":junit_runtime",
    ],
    deps = [
        "//examples/junit/src/main/java/com/example:parser",
        "//src/main/java/com/code_intelligence/jazzer/junit:fuzz_test",
        "@maven//:org_junit_jupiter_junit_jupiter_api",
    ],
)

java_fuzz_target_test(
    name = "ByteFuzzTest",
    srcs = ["ByteFuzzTest.java"],
    allowed_findings = ["org.opentest4j.AssertionFailedError"],
    fuzzer_args = [
        "-runs=0",
    ],
    target_class = "com.example.ByteFuzzTest",
    target_method = "byteFuzz",
    verify_crash_reproducer = False,
    runtime_deps = [
        ":junit_runtime",
    ],
    deps = [
        "//examples/junit/src/main/java/com/example:parser",
        "//src/main/java/com/code_intelligence/jazzer/junit:fuzz_test",
        "@maven//:org_junit_jupiter_junit_jupiter_api",
    ],
)

java_fuzz_target_test(
    name = "PerExecutionLifecycleFuzzTest",
    srcs = ["PerExecutionLifecycleFuzzTest.java"],
    expect_crash = True,
    expected_warning_or_error = "ERROR: com.example.TestSuccessfulException: Lifecycle methods invoked as expected",
    fuzzer_args = [
        "-runs=3",
    ],
    target_class = "com.example.PerExecutionLifecycleFuzzTest",
    verify_crash_input = False,
    verify_crash_reproducer = False,
    runtime_deps = [
        ":junit_runtime",
    ],
    deps = [
        ":lifecycle_recording_test_base",
        ":test_successful_exception",
        "//examples/junit/src/main/java/com/example:parser",
        "//src/main/java/com/code_intelligence/jazzer/junit:fuzz_test",
        "@maven//:com_google_truth_extensions_truth_java8_extension",
        "@maven//:com_google_truth_truth",
        "@maven//:org_junit_jupiter_junit_jupiter_api",
    ],
)

java_fuzz_target_test(
    name = "PerExecutionLifecycleWithFindingFuzzTest",
    srcs = ["PerExecutionLifecycleWithFindingFuzzTest.java"],
    allowed_findings = ["java.io.IOException"],
    expected_warning_or_error = "ERROR: com.example.TestSuccessfulException: Lifecycle methods invoked as expected",
    fuzzer_args = [
        "-runs=3",
    ],
    target_class = "com.example.PerExecutionLifecycleWithFindingFuzzTest",
    verify_crash_reproducer = False,
    runtime_deps = [
        ":junit_runtime",
    ],
    deps = [
        ":lifecycle_recording_test_base",
        ":test_successful_exception",
        "//examples/junit/src/main/java/com/example:parser",
        "//src/main/java/com/code_intelligence/jazzer/junit:fuzz_test",
        "@maven//:com_google_truth_extensions_truth_java8_extension",
        "@maven//:com_google_truth_truth",
        "@maven//:org_junit_jupiter_junit_jupiter_api",
    ],
)

java_fuzz_target_test(
    name = "PerTestLifecycleFuzzTest",
    srcs = ["PerTestLifecycleFuzzTest.java"],
    expect_crash = True,
    expected_warning_or_error = "ERROR: com.example.TestSuccessfulException: Lifecycle methods invoked as expected",
    fuzzer_args = [
        "-runs=3",
    ],
    target_class = "com.example.PerTestLifecycleFuzzTest",
    verify_crash_input = False,
    verify_crash_reproducer = False,
    runtime_deps = [
        ":junit_runtime",
    ],
    deps = [
        ":lifecycle_recording_test_base",
        ":test_successful_exception",
        "//examples/junit/src/main/java/com/example:parser",
        "//src/main/java/com/code_intelligence/jazzer/junit:fuzz_test",
        "@maven//:com_google_truth_extensions_truth_java8_extension",
        "@maven//:com_google_truth_truth",
        "@maven//:org_junit_jupiter_junit_jupiter_api",
    ],
)

java_fuzz_target_test(
    name = "KeepGoingFuzzTest",
    srcs = ["KeepGoingFuzzTest.java"],
    allowed_findings = ["java.lang.IllegalArgumentException"],
    expect_crash = False,
    fuzzer_args = [
        "--keep_going=3",
        "-runs=10",
    ],
    target_class = "com.example.KeepGoingFuzzTest",
    runtime_deps = [
        ":junit_runtime",
    ],
    deps = [
        "//src/main/java/com/code_intelligence/jazzer/junit:fuzz_test",
        "@maven//:org_junit_jupiter_junit_jupiter_api",
    ],
)

# Verifies that fuzzer command-line arguments are honored for @FuzzTests.
java_fuzz_target_test(
    name = "CommandLineFuzzTest",
    srcs = ["CommandLineFuzzTest.java"],
    allowed_findings = ["java.lang.Error"],
    fuzzer_args = [
        # Ignore the first two findings.
        "--ignore=d5e250a5298b81e6,d86371e6d41739ec",
    ],
    target_class = "com.example.CommandLineFuzzTest",
    verify_crash_reproducer = False,
    runtime_deps = [
        ":junit_runtime",
    ],
    deps = [
        "//examples/junit/src/main/java/com/example:parser",
        "//src/main/java/com/code_intelligence/jazzer/junit:fuzz_test",
        "@maven//:org_junit_jupiter_junit_jupiter_api",
    ],
)

# Verify that Mockito is properly ignored.
# Using version 5+ could otherwise introduce cyclic instrumentation.
java_fuzz_target_test(
    name = "MockitoFuzzTest",
    srcs = ["MockitoFuzzTest.java"],
    fuzzer_args = [
        "-runs=1",
    ],
    tags = ["no-jdk8"],
    target_class = "com.example.MockitoFuzzTest",
    runtime_deps = [
        ":junit_runtime",
    ],
    deps = [
        "//examples/junit/src/main/java/com/example:parser",
        "//src/main/java/com/code_intelligence/jazzer/junit:fuzz_test",
        "@maven//:org_junit_jupiter_junit_jupiter_api",
        "@maven//:org_mockito_mockito_core",
    ],
)

java_fuzz_target_test(
    name = "AutofuzzLifecycleFuzzTest",
    srcs = ["AutofuzzLifecycleFuzzTest.java"],
    fuzzer_args = [
        "-runs=0",
    ],
    target_class = "com.example.AutofuzzLifecycleFuzzTest",
    runtime_deps = [
        ":junit_runtime",
    ],
    deps = [
        "//src/main/java/com/code_intelligence/jazzer/junit:fuzz_test",
        "@maven//:org_junit_jupiter_junit_jupiter_api",
    ],
)

java_fuzz_target_test(
    name = "MutatorFuzzTest",
    srcs = ["MutatorFuzzTest.java"],
    allowed_findings = ["java.lang.AssertionError"],
    data = [
        "//examples/junit/src/test/resources:MutatorFuzzTestInputs",
    ],
    env = {
        "JAZZER_FUZZ": "1",
    },
    fuzzer_args = [
        "--experimental_mutator",
        "$(rlocationpaths //examples/junit/src/test/resources:MutatorFuzzTestInputs)",
    ],
    target_class = "com.example.MutatorFuzzTest",
    verify_crash_reproducer = False,
    runtime_deps = [
        ":junit_runtime",
    ],
    deps = [
        "//src/main/java/com/code_intelligence/jazzer/driver:fuzz_target_runner",
        "//src/main/java/com/code_intelligence/jazzer/junit:fuzz_test",
        "//src/main/java/com/code_intelligence/jazzer/mutation/annotation",
        "@maven//:org_junit_jupiter_junit_jupiter_api",
    ],
)

java_fuzz_target_test(
    name = "JavaSeedFuzzTest",
    srcs = ["JavaSeedFuzzTest.java"],
    allowed_findings = ["java.lang.Error"],
    env = {"JAZZER_FUZZ": "1"},
    fuzzer_args = [
        "--experimental_mutator",
    ],
    target_class = "com.example.JavaSeedFuzzTest",
    verify_crash_reproducer = False,
    runtime_deps = [
        ":junit_runtime",
    ],
    deps = [
        "//src/main/java/com/code_intelligence/jazzer/junit:fuzz_test",
        "//src/main/java/com/code_intelligence/jazzer/mutation/annotation",
        "@maven//:org_junit_jupiter_junit_jupiter_api",
        "@maven//:org_junit_jupiter_junit_jupiter_params",
    ],
)

java_fuzz_target_test(
    name = "JavaBinarySeedFuzzTest",
    srcs = ["JavaBinarySeedFuzzTest.java"],
    allowed_findings = ["java.lang.Error"],
    env = {"JAZZER_FUZZ": "1"},
    target_class = "com.example.JavaBinarySeedFuzzTest",
    verify_crash_reproducer = False,
    runtime_deps = [
        ":junit_runtime",
    ],
    deps = [
        "//src/main/java/com/code_intelligence/jazzer/junit:fuzz_test",
        "@maven//:org_junit_jupiter_junit_jupiter_api",
        "@maven//:org_junit_jupiter_junit_jupiter_params",
    ],
)

[
    java_fuzz_target_test(
        name = "DictionaryFuzzTests_" + method,
        srcs = ["DictionaryFuzzTests.java"],
        allowed_findings = ["com.example.TestSuccessfulException"],
        env = {"JAZZER_FUZZ": "1"},
        target_class = "com.example.DictionaryFuzzTests",
        target_method = method,
        verify_crash_reproducer = False,
        runtime_deps = [
            ":junit_runtime",
        ],
        deps = [
            "//examples/junit/src/test/java/com/example:test_successful_exception",
            "//examples/junit/src/test/resources:example_dictionaries",
            "//src/main/java/com/code_intelligence/jazzer/junit:fuzz_test",
            "@maven//:org_junit_jupiter_junit_jupiter_api",
            "@maven//:org_junit_jupiter_junit_jupiter_params",
        ],
    )
    for method in [
        "inlineTest",
        "fileTest",
        "mixedTest",
    ]
]

java_library(
    name = "lifecycle_recording_test_base",
    testonly = True,
    srcs = ["LifecycleRecordingTestBase.java"],
    deps = [
        "//src/main/java/com/code_intelligence/jazzer/junit:fuzz_test",
        "@maven//:com_google_truth_extensions_truth_java8_extension",
        "@maven//:com_google_truth_truth",
        "@maven//:org_junit_jupiter_junit_jupiter_api",
    ],
)

java_library(
    name = "junit_runtime",
    runtime_deps = [
        "@maven//:org_junit_jupiter_junit_jupiter_engine",
        "@maven//:org_junit_platform_junit_platform_launcher",
    ],
)
